#!/bin/sh
echo $0 $*
./pic2fb loading.png 

GAMEDIR=${PWD}
echo ==========GAMEDIR is $GAMEDIR

#only dirname, SAME
DIRNAME=${GAMEDIR##*/}
DIRNAME=$(basename ${PWD})
echo game dir name is:  $DIRNAME

PORT_DIR=${GAMEDIR%/*}
echo ==========parent is PORTDIR: $PORT_DIR

controlfolder="../PortMaster"
source $controlfolder/control.txt
#source $controlfolder/tasksetter
get_controls

gamedir="$GAMEDIR"
cd "$gamedir/gamedata"

# Check if it's the Windows or Linux version
if [[ -f "Stardew Valley.exe" ]]; then
	gameassembly="Stardew Valley.exe"

	# Copy the Windows Stardew Valley WinAPI workarounds
	cp "${gamedir}/dlls/Stardew Valley.exe.config" "${gamedir}/gamedata/Stardew Valley.exe.config"
else
	gameassembly="StardewValley.exe"
fi

# Grab text output...
$ESUDO chmod 666 /dev/tty0
printf "\033c" > /dev/tty0
echo "Loading... Please Wait." > /dev/tty0

# Setup mono
monodir="$HOME/mono"
monofile="$controlfolder/libs/mono-6.12.0.122-aarch64.squashfs"
$ESUDO mkdir -p "$monodir"
$ESUDO umount "$monodir" || true
$ESUDO mount -t squashfs "$monofile" "$monodir"

# Setup savedir
$ESUDO rm -rf ~/.config/StardewValley
ln -sfv "$gamedir/savedata" ~/.config/StardewValley

# Remove all the dependencies in favour of system libs - e.g. the included
# newer version of MonoGame with fixes for SDL2
rm -f System*.dll MonoGame*.dll mscorlib.dll

# Setup path and other environment variables
export MONOGAME_PATCH="$gamedir/dlls/StardewPatches.dll"
export MONO_PATH="$gamedir/dlls":"$gamedir"
export PATH="$monodir/bin":"$PATH"
export LD_LIBRARY_PATH="$gamedir/libs:$LD_LIBRARY_PATH"
export LIBGL_ES=2
export LIBGL_GL=21
export LIBGL_FB=4
export SDL_VIDEO_GL_DRIVER="$gamedir/libs/libGL.so.1"
export SDL_VIDEO_EGL_DRIVER="$gamedir/libs/libEGL.so.1"

$GPTOKEYB "mono" &
$TASKSET mono ../SVLoader.exe "${gameassembly}" 2>&1 | tee "${gamedir}/log.txt"
$ESUDO kill -9 $(pidof gptokeyb)
$ESUDO systemctl restart oga_events &
$ESUDO umount "$monodir"

# Disable console
printf "\033c" >> /dev/tty1
